from flask import Flask, jsonify, request  
import smtplib  
from email.mime.multipart import MIMEMultipart  
from email.mime.text import MIMEText  
from flask_cors import CORS  
  
app = Flask(__name__)  
cors = CORS(app, resources={r"/*": {"origins": "*"}}) 
  
# 假设我们有一个简单的用户数据列表  
users = [  
    {'id': 1, 'name': 'Alice', 'age': 25},  
    {'id': 2, 'name': 'Bob', 'age': 30},  
]  
  
# 获取所有用户  
@app.route('/notify', methods=['GET'])  
def get_users():  
    smtp_server = 'smtp.qq.com'  
    port = 587  # 通常为587或465，取决于你的SMTP服务器配置  
      
    # 设置发件人和收件人的电子邮件地址  
    sender_email = '282126869@qq.com'  
    receiver_email = 'gd@tongji.edu.cn'  
    password = 'efzfcnogfccocbce'  # 你的电子邮件密码或应用专用密码  
      
    # 创建一个MIMEMultipart对象并设置其属性  
    msg = MIMEMultipart()  
    msg['From'] = sender_email  
    msg['To'] = receiver_email  
    msg['Subject'] = 'Nexus Warning: Caught a Violation!'  
      
    # 添加邮件正文  
    body = 'Dear Sir:\n Nexus has caught a violation, please login to dashboard to see details and take actions as soon as possible!\n\n Nexus'  
    msg.attach(MIMEText(body, 'plain'))  
      
    # 创建一个SMTP对象并连接到SMTP服务器  
    with smtplib.SMTP(smtp_server, port) as server:  
        # 启动TLS模式（如果SMTP服务器支持）  
        server.starttls()  
        # 登录到你的电子邮件账户  
        server.login(sender_email, password)  
        # 发送电子邮件  
        text = msg.as_string()  
        server.sendmail(sender_email, receiver_email, text)
    return jsonify(users)  
  
# 运行服务器  
if __name__ == '__main__':  
    print('run')
    app.run(port=3000, debug=True)